package com.zhuoyue.servlet;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;

public class SmartDownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        SmartUpload smart=new SmartUpload();
        smart.setContentDisposition(null);//什么不设置 就是下载
        smart.initialize(this.getServletConfig(),request,response);
//        String path = request.getRealPath("files/logo.png");
//        String path = request.getRealPath("files/logo.png");
//        String path = request.getRealPath("files/logo.png");
        String path = request.getParameter("path");
        System.out.println(path);
        String name = new File(path).getName();
        try {
            smart.downloadFile(path,"",name);
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
    }
}
